package com.cya.forumproject.controller;

import com.cya.forumproject.common.AppResult;
import com.cya.forumproject.common.ResultCode;
import com.cya.forumproject.config.AppConfig;
import com.cya.forumproject.model.Article;
import com.cya.forumproject.model.ArticleReply;
import com.cya.forumproject.model.User;
import com.cya.forumproject.services.IArticleReplyService;
import com.cya.forumproject.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/9/25 17:35
 * @description：
 * @modified By：
 * @version:
 */
@Slf4j
@RestController
@RequestMapping("/reply")
@Api(tags = "回复接口")
public class ArticleReplyController {

    @Resource
    private IArticleReplyService iArticleReplyService;

    @Resource
    private IArticleService iArticleService;

    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(@ApiParam("帖子id")@RequestParam("articleId")@NonNull Long articleId,
                            @ApiParam("帖子内容")@RequestParam("content")@NonNull String content,
                            HttpServletRequest request){
        HttpSession session=request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);

        //用户已经禁言
        if (user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //获取帖子信息
        Article article = iArticleService.selectById(articleId);

//        帖子是否存在或已删除
        if (article==null||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
//        是否封贴
        if (article.getState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        ArticleReply article1=new ArticleReply();
        article1.setArticleId(articleId);
        article1.setContent(content);
        article1.setPostUserId(user.getId());
        iArticleReplyService.create(article1);
        return AppResult.success();
    }


    @ApiOperation("获得回复帖子列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam("帖子id")@RequestParam("articleId")@NonNull Long articleId)

    {
        Article article = iArticleService.selectById(articleId);
        //帖子不存才
        if (article==null||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        List<ArticleReply> articleReplies = iArticleReplyService.selectByArticleId(articleId);

return AppResult.success(articleReplies);
    }

}